package cn.rongdao.jrkabao.ui.takephoto;

import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.util.FRImageLoad;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frtools.util.FRToast;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.FRBaseTakePhotoActivity;
import cn.rongdao.jrkabao.bean.takephoto.OtherImageDataBean;
import cn.rongdao.jrkabao.dao.UserDao;
import cn.rongdao.jrkabao.model.takephoto.OtherImageFileAddPhotoModel;
import cn.rongdao.jrkabao.model.takephoto.OtherImageFileGetListModel;
import cn.rongdao.jrkabao.ui.takephoto.adapter.OtherImageDataAdapter;

/**
 * Created by jiangyongxing on 2017/2/20.
 * 描述：
 */

public class OtherImageDataActivity extends FRBaseTakePhotoActivity {

    private List<OtherImageDataBean> datas = new ArrayList<>();
    private OtherImageDataAdapter mOtherImageDataAdapter;
    private TitleBarUIComponent titleBarUIComponent;
    private ImageView mImageView;
    private int mConfigId = 0;
    private Map<String, String> mMap;


    @Override
    protected int layoutRes() {
        return R.layout.activity_other_image_data;
    }

    @Override
    protected void initView() {
        super.initView();
        initData();
        titleBarUIComponent = (TitleBarUIComponent) findViewById(R.id.aoid_titlebar);
        initTitleBar();
        RecyclerView recyclerview = (RecyclerView) findViewById(R.id.activity_other_image_data_recyclerview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerview.setLayoutManager(linearLayoutManager);
        mOtherImageDataAdapter = new OtherImageDataAdapter(datas);
        recyclerview.setAdapter(mOtherImageDataAdapter);
        mOtherImageDataAdapter.setOnItemActionListener(new OtherImageDataAdapter.OnItemActionListener() {
            @Override
            public void onClickPicSend(int pos, ImageView itemPic, int configId) {
                if (!lackPermission()) {
                    try {
                        takePhoto(false, -1);
                        mImageView = itemPic;
                        mConfigId = configId;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    private void initTitleBar() {
        titleBarUIComponent.setBackgroundColor(Color.WHITE);
        titleBarUIComponent.initTitleBlack("其他影像资料");
        titleBarUIComponent.initLeftBackBlack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    protected void initData() {
        String mUserid = UserDao.getInstance().getUserId();
        String mToken = UserDao.getInstance().getToken();
        mMap = new HashMap<>();
        requestData();
    }

    private void requestData() {
        OtherImageFileGetListModel.getInstance().execute(mMap, new FRBaseObserver<List<OtherImageDataBean>>(this, "正在加载") {
            @Override
            protected void onBaseNext(List<OtherImageDataBean> data) {
                datas.clear();
                datas.addAll(data);
                mOtherImageDataAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void successReturnPicpath(String picPath, String suffix, String imageviewBase64) {
        mMap.put("suffix", FRString.valueOf(suffix));
        FRImageLoad.loadImage(picPath, mImageView);
        if (TextUtils.isEmpty(imageviewBase64)) {
            Toast.makeText(this, "选择图片文件不正确", Toast.LENGTH_LONG).show();
            return;
        }
        mMap.put("profile_pic", FRString.valueOf(imageviewBase64));
        sendImageData();

    }

    private void sendImageData() {
        mMap.put("config_id", String.valueOf(mConfigId));

        OtherImageFileAddPhotoModel.getInstance().execute(mMap, new FRBaseObserver<String>(this) {
            @Override
            protected void onBaseNext(String data) {
                FRToast.showToastSafe("上传成功");
            }
        });
    }
}
